<?php
class AdPositionSaveEditAction extends AdminAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'json';
	}
	
	protected function afterConstruct()
	{
		$this->parseDtds('ad','position');
	}
	
	public function execute()
	{
		$id = $this->context->post('id');
		
		$data = $this->getValidData();
		
		
		$adPositionService = new BaseDbService('ad_position');
		
		$tmp = $adPositionService->fetch(array('template'=>'eq:'.$data['template'],'position'=>'eq:'.$data['position']));
		if($tmp && $tmp['id']!=$id)
		{
			$this->data['error_messages']['position'] = array('exists'=>'该广告位置别名已经存在');
		}
		
		if(empty($this->data['error_messages']))
		{
			if(is_numeric($id) && $id)
			{
				$adPositionService->update($id, $data);
				$this->code	= 200;
			}
			else
			{
				$id	= $adPositionService->add($data);
				if($id)
				{
					$this->code	= 200;
				}
				else
				{
					$this->code	= 400;
				}
			}
			
			AdminUtil::clearAdCache();
		}
		else
		{
			$this->code = 500;
		}
		
		$this->data['forward'] = C('LtUrl')->generate('Ad', 'Position', array('template'=>$data['template']));
	}
}
